// SPDX-FileCopyrightText: © 2025 Tenstorrent AI ULC
//
// SPDX-License-Identifier: Apache-2.0

#include <tt_stl/assert.hpp>
#include <tt-metalium/experimental/lightmetal/lightmetal_binary.hpp>
#include <tt-metalium/experimental/lightmetal/lightmetal_replay.hpp>
#include <tt-metalium/experimental/lightmetal/lightmetal_api.hpp>
#include <tt-logger/tt-logger.hpp>
#include <string>
#include <utility>

using namespace tt;

// This is a standalone tool for executing Light Metal Binary files. Light Metal Binary files
// are generated by the LightMetalBeginCapture() and LightMetalEndCapture() APIs and contain
// a serialized representation of
//  - Host API calls
//  - Device CQ workload/traces
//  - (Future support) Precompiled programs/kernels for fast deployment
//
// Usage:
//   lightmetal_runner <binary_file>
//
// Arguments:
//   <binary_file> - Path to the Light Metal Binary file to be executed.
//
// This tool reads the specified binary file, transfers ownership of its contents to the
// light metal binary replay executor, and runs it, returning pass (0) or fail (1).

int main(int argc, char* argv[]) {
    using namespace tt::tt_metal::experimental::lightmetal;

    // Process cmdline arguments
    std::string program_filename = argv[0];
    TT_FATAL(argc == 2, "Invalid number of supplied arguments. Usage: {} <binary_file>", program_filename.c_str());
    std::string binary_filename = argv[1];

    // Read the Light Metal Binary file into blob, transfer ownership and execute it.
    auto binary = LightMetalBinary::load_from_file(binary_filename);
    LightMetalReplay lm_replay(std::move(binary));

    if (!lm_replay.run()) {
        log_fatal(tt::LogMetalTrace, "Light Metal Binary {} failed to execute or encountered errors.", binary_filename);
        return 1;
    } else {
        log_info(tt::LogMetalTrace, "Light Metal Binary {} executed successfully", binary_filename);
        return 0;
    }
}
